import java.util.Arrays;
import java.util.Scanner;

public class AG2 {

	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int count = scan.nextInt();
        while (count-- > 0) {
            int[][] sqars = new int[3][2];
            for (int i = 0; i < 6; i++) {
            	sqars[i / 2][i % 2] = scan.nextInt();
            }
            int min = 8;
            for (int s1 = 0; s1 < sqars.length; s1++) {
            	for (int p1 = 0; p1 < sqars[s1].length; p1++) {
            		
            		for (int s2 = 0; s2 < sqars.length; s2++) {
            			if (s1 == s2) continue;
            			for (int p2 = 0; p2 < sqars[s2].length; p2++) {
            				
            				for (int s3 = 0; s3 < sqars.length; s3++) {
            					if (s2 == s3 || s1 == s3) continue;
            					for (int p3 = 0; p3 < sqars[s3].length; p3++) {
            						min = Math.min(min, getMin(s1, p1, s2, p2, s3, p3, sqars));
            					}
            				}
            			}
            		}
            	}
            }
            
            
            System.out.println(min);
        }
        
        scan.close();
    }


	public static int getMin(int s1, int p1, int s2, int p2, int s3, int p3, int[][] sqars) {
    	int[][] tmp = new int[sqars.length][sqars[0].length];
    	tmp[0][0] = sqars[s1][p1];
    	tmp[0][1] = sqars[s1][sqars[0].length - 1 - p1];
    	tmp[1][0] = sqars[s2][p2];
    	tmp[1][1] = sqars[s2][sqars[1].length - 1 - p2];
    	tmp[2][0] = sqars[s3][p3];
    	tmp[2][1] = sqars[s3][sqars[2].length - 1 - p3];
    	if (tmp[0][0] == tmp[1][0] && tmp[2][0] == tmp[1][0]) return 4;
    	if (tmp[0][0] == tmp[1][0] || tmp[2][0] == tmp[1][0]) return 6;
    	if (tmp[0][0] == tmp[1][0] + tmp[2][0]) {
    		if (tmp[1][1] == tmp[2][1]) return 4;
    		return 6;
    	}
    	if (tmp[1][1] == tmp[2][1]) return 6;
    	return 8;
    }
}
